﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using LEDHelper;
namespace LEDDataBase
{
    //该数据库保存了所有的数据，需要时，保存和取出。各单元维护自己的数据。
    public partial class LEDData
    {
        string user
        {
            get
            {
                return "SXDLED";
            }
        }
        string Date
        {
            get
            {
                return System.DateTime.Now.Year.ToString()+System.DateTime.Now.Month.ToString()+System.DateTime.Now.Day.ToString();
            }
        }
        public DataRow AddPrograme(string  system, int led,int playtype,int nScreenType,int msgtype)
        {
            DataRow dwResult = null;
            try
            {
                //ROWID	PRSYSTEM	PRLEDID	PRPROGRAMID	PRPROGRAMNAME	PRPROGDES	PRPLAYTYPE	PRSCREENTYPE	PRDISPLAYTIME	PRMAXLEFID	PRNEWDATE	PRNEWUSER
                string pid = Guid.NewGuid().ToString();
                pid = msgtype.ToString();
                var query = from dwPr in LEDDatas.PROGRAM.AsEnumerable()
                            where dwPr["PRPROGRAMID"].ToString() == pid
                            select dwPr;
                foreach (DataRow dwTemp in query)
                {
                    dwResult = dwTemp;
                }
                if (dwResult == null)
                {
                    dwResult = LEDDatas.PROGRAM.Rows.Add();
                }
                dwResult["ROWID"] = pid;
                dwResult["PRSYSTEM"] = system;
                dwResult["PRLEDID"] = led;
                dwResult["PRPROGRAMID"] = pid;
                dwResult["PRPROGRAMNAME"] = pid;
                dwResult["PRPROGDES"] = 1;
                dwResult["PRPLAYTYPE"] = playtype;
                dwResult["PRSCREENTYPE"] = nScreenType;
                dwResult["PRDISPLAYTIME"] = Date;
                dwResult["PRMAXLEFID"] = 1;
                dwResult["NEWDATE"] = Date;
                dwResult["NEWUSER"] = user;
                dwResult["LASTDATE"] = Date;
                dwResult["LASTUSER"] = user;  

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return dwResult;
        }
        public DataRow AddLeaf(DataRow dwInput,int lDisplayTime,string leafid)
        {
            DataRow dwResult = null;
            try
            {
                var query = from dwPr in LEDDatas.LEAF.AsEnumerable()
                            where dwPr["LFLEAFID"].ToString() == leafid
                            select dwPr;
                foreach (DataRow dwTemp in query)
                {
                    dwResult = dwTemp;
                }
                if (dwResult == null)
                {
                    dwResult = LEDDatas.LEAF.Rows.Add();
                }
                //string pid = Guid.NewGuid().ToString();
                dwResult["ROWID"] = dwInput["ROWID"];
                dwResult["LFSYSTEM"] = dwInput["PRSYSTEM"];
                dwResult["LFLEDID"] = dwInput["PRLEDID"];
                dwResult["LFPROGRAMID"] = dwInput["PRPROGRAMID"];
                dwResult["LFLEAFID"] = leafid;
                dwResult["LFLEAFNAME"] = leafid;
                dwResult["LFDURATION"] = lDisplayTime;
                dwResult["LFLEAFDES"] = leafid;
                dwResult["NEWDATE"] = Date;
                dwResult["NEWUSER"] = user;
                dwResult["LASTDATE"] = Date;
                dwResult["LASTUSER"] = user;   
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return dwResult;
        }
        public DataRow AddLeafDetail(DataRow dwInput, string ctype, string contentid, SRECT rectx)
        {
            DataRow dwResult = null;
            try
            {
                var query = from dwPr in LEDDatas.LEAFDETAIL.AsEnumerable()
                            where dwPr["LDCONTENTID"].ToString() == contentid
                            select dwPr;
                foreach (DataRow dwTemp in query)
                {
                    dwResult = dwTemp;
                }
                if (dwResult == null)
                {
                    dwResult = LEDDatas.LEAFDETAIL.Rows.Add();
                }
               int left,top,right,bottom;
               left = rectx.left;
               right = rectx.right;
               top = rectx.top;
               bottom = rectx.bottom;
              
                dwResult["ROWID"] = dwInput["ROWID"];
                dwResult["LDSYSTEM"] = dwInput["LFSYSTEM"];
                dwResult["LDLEDID"] = dwInput["LFLEDID"];
                dwResult["LDPROGRAMID"] = dwInput["LFPROGRAMID"];
                dwResult["LDLEAFID"] = dwInput["LFLEAFID"];
                dwResult["LDAREAID"] = 1;
                dwResult["LDCONTENTTYPE"] = ctype;
                dwResult["LDCONTENTID"] = contentid;
                dwResult["LDLTOPX"] = left;
                dwResult["LDLTOPY"] = top;

                dwResult["LDLBOTTOMX"] = left;
                dwResult["LDLBOTTOMY"] = bottom;

                dwResult["LDRTOPX"] = right;
                dwResult["LDRTOPY"] = top;

                dwResult["LDRBOTTOMX"] = right;
                dwResult["LDRBOTTOMY"] = bottom;

                dwResult["NEWDATE"] = Date;
                dwResult["NEWUSER"] = user;
                dwResult["LASTDATE"] = Date;
                dwResult["LASTUSER"] = user;  
                	
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return dwResult;
        }
        public DataRow AddText(string system, string szText, string fontid, string filepath, int method, int speed, int transparent, string contentid)
        {
            DataRow dwResult = null;
            try
            {
                var query = from dwPr in LEDDatas.CTEXT.AsEnumerable()
                            where dwPr["CONTENTID"].ToString() == contentid
                            select dwPr;
                foreach (DataRow dwTemp in query)
                {
                    dwResult = dwTemp;
                }
                if (dwResult == null)
                {
                    dwResult = LEDDatas.CTEXT.Rows.Add();
                }
                dwResult["ROWID"] = contentid;
                dwResult["CTSYSTEM"] = system;
                dwResult["CONTENTID"] = contentid;
                dwResult["CTCONTENT"] = szText;
                dwResult["CTEXTFILE"] = filepath;
                dwResult["FONTID"] = fontid;
                dwResult["EFFECT"] = method;
                dwResult["SPEED"] = speed;
                dwResult["TRANSPARENT"] = transparent;

                dwResult["NEWDATE"] = Date;
                dwResult["NEWUSER"] = user;
                dwResult["LASTDATE"] = Date;
                dwResult["LASTUSER"] = user;  
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return dwResult;
        }
        public DataRow AddPicture(string system, string filepath, int width, int height, int method, int speed, int transparent,string contentid)
        {
            DataRow dwResult = null;
            try
            {
                var query = from dwPr in LEDDatas.CPICTURE.AsEnumerable()
                            where dwPr["CONTENTID"].ToString() == contentid
                            select dwPr;
                foreach (DataRow dwTemp in query)
                {
                    dwResult = dwTemp;
                }
                if (dwResult == null)
                {
                    dwResult = LEDDatas.CPICTURE.Rows.Add();
                }
                dwResult["ROWID"] = contentid;
                dwResult["CPSYSTEM"] = system;
                dwResult["CONTENTID"] = contentid;
                dwResult["CPPICTUREAME"] = contentid;
                dwResult["CPPICTUREFILE"] = filepath;
                dwResult["CPWIDTH"] = width;
                dwResult["CPHEIGHT"] = height;
                dwResult["EFFECT"] = method;
                dwResult["SPEED"] = speed;
                dwResult["TRANSPARENT"] = transparent;
                dwResult["NEWDATE"] = Date;
                dwResult["NEWUSER"] = user;
                dwResult["LASTDATE"] = Date;
                dwResult["LASTUSER"] = user;  
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return dwResult;
        }
        public DataRow AddFont(string system, string fontname, int fontsize, int fontcolor, int italic, int bold, int underline, int method)
        {
            DataRow dwResult = null;
            try
            {
                //ROWID	CFSYSTEM	CFFONTID	CFFONTNAME	CFFONTTYPE	CFFONTSIZE	CFCOLOR	CFFONTITALIC	CFFONTBOLD	CFFONTUNDERLINE	CFFONEFFECT	CFNEWDATE	CFNEWUSER
                string pid = Guid.NewGuid().ToString();
                dwResult = LEDDatas.FONTTABLE.Rows.Add();
                dwResult["ROWID"] = pid;
                dwResult["CFSYSTEM"] = system;
                dwResult["CONTENTID"] = pid;
                dwResult["CFFONTNAME"] = fontname;
                dwResult["CFFONTTYPE"] = fontname;
                dwResult["CFFONTSIZE"] = fontsize;
                dwResult["CFCOLOR"] = fontcolor;
                dwResult["CFFONTITALIC"] = italic;
                dwResult["CFFONTBOLD"] = bold;
                dwResult["CFFONTUNDERLINE"] = underline;
                dwResult["CFFONEFFECT"] = method;
                dwResult["NEWDATE"] = Date;
                dwResult["NEWUSER"] = user;
                dwResult["LASTDATE"] = Date;
                dwResult["LASTUSER"] = user;  
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return dwResult;
        }
        public DataRow AddClock(string system, string fontid, string cktype, int method, int speed, int transparent, string contentid)
        {
            DataRow dwResult = null;
            try
            {
                var query = from dwPr in LEDDatas.CCLOCK.AsEnumerable()
                            where dwPr["CONTENTID"].ToString() == contentid
                            select dwPr;
                foreach (DataRow dwTemp in query)
                {
                    dwResult = dwTemp;
                }
                if (dwResult == null)
                {
                    dwResult = LEDDatas.CCLOCK.Rows.Add();
                }
                dwResult["ROWID"] = contentid;
                dwResult["CKSYSTEM"] = system;
                dwResult["CONTENTID"] = contentid;
                dwResult["FONTID"] = fontid;
                dwResult["CCTYPE"] = cktype;
                dwResult["EFFECT"] = method;
                dwResult["SPEED"] = speed;
                dwResult["TRANSPARENT"] = transparent;
                dwResult["NEWDATE"] = Date;
                dwResult["NEWUSER"] = user;
                dwResult["LASTDATE"] = Date;
                dwResult["LASTUSER"] = user;  
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return dwResult;
        }
        public DataRow GetContent(string table, string contentid)
        {
            DataRow dwContent = null;
            try
            {
                string filter = "CONTENTID='" + contentid + "'";
                DataRow[] rows = LEDDatas.Tables[table].Select(filter);
                if (rows.Length > 0)
                {
                    dwContent = rows[0];
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return dwContent;
        }
    }
}
